# -*- coding:utf-8 -*-

__author__ = 'jie.zhang@dmall.com'
import sys
import urllib
import urllib2
import json
from sys import argv
import time

import os
import logging


reload(sys)
print sys.getdefaultencoding()
sys.setdefaultencoding('utf-8')


"""
发送http请求
"""
def generate_url(host_port,path,params):
    test_url = 'http://'+host_port+path+"?"+urllib.urlencode(params)
    return test_url

def send_http_request(host,bcode,keyword):
    try:
        path = "/ware/multiStoreWares"
        params = {
            "pageNum": 1,
            "pageSize": 1
        }

        if bcode==1:
            params["vsPairs"]="1-112-124"
            params["isShowPreSell"]=0
        elif bcode==2:
            params["vsPairs"] = "1-112-124"
            params["isShowPreSell"] = 2

        params["userId"] = 327248
        params["bCode"] = bcode
        params["keyword"] = keyword.strip()
        url = generate_url(host, path, params)
        print url
        response = urllib2.urlopen(url)
        result = json.loads(response.read())
        if result.has_key('data'):
            if result["data"].has_key("pageInfo"):
                total = result["data"]["pageInfo"]["total"]
                return total
            else:
                return "No"
    except Exception as err:
        return "exception"
        #logging.exception(err)
if __name__ == "__main__":
    # host_port_arr = ["searchapi.dmall.com","10.248.209.228:8010"]
    file_object = open("/Users/u01/workspace/beimingtianhe/pythondemo/keyword_noresult","r")

    for line in file_object:
        info = ""
        t1 = time.time()
        info += str(send_http_request("10.248.210.210:8011",1,line))+" "
        t2 = time.time()
        info += '%.2f' % (t2-t1)+";      "

        info += str(send_http_request("10.248.209.228:8010", 1, line))+" "
        t3 = time.time()
        info += '%.2f' % (t3 - t2) + ";        "

        info += str(send_http_request("10.248.210.210:8011", 2, line))+" "
        t4 = time.time()
        info += '%.2f' % ((t4 - t3)) + ";        "

        info += str(send_http_request("10.248.209.228:8010", 2, line))+" "
        t4 = time.time()
        info += '%.2f' % (t4 - t3) + ";        "
        info += line.replace("\n", "") + "   "
        print info